package cn.jimmie.learn.viewArt;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.jimmie.learn.viewArt.refreshview.RefreshLayout;
import cn.jimmie.learn.viewArt.refreshview.core.RefreshState;


/**
 * function :
 *
 * <p> Create Time:  2019年04月13日 13:54 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class RecyclerRefreshViewActivity extends AppCompatActivity {

    RefreshLayout layout;
    RecyclerView recyclerView;
    TextView emptyView;
    String[] itemsTmp =
//            {"A", "B", "C", "D", "E", "F", "G", "H"};
                {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"};
    List<String> items = new ArrayList<>(Arrays.asList(itemsTmp));
    MyAdapter adapter;

    int count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler_refresh_activity);

        layout = findViewById(R.id.refresh_layout);
        recyclerView = findViewById(R.id.list_view);
        emptyView = findViewById(R.id.empty_view);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        layout.setFootViewEnable(false);
        layout.setHeadViewEnable(true);

        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter = new MyAdapter(items);
        recyclerView.setAdapter((adapter));

        layout.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onPullRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RecyclerRefreshViewActivity.this, "头部刷新结束", Toast.LENGTH_SHORT).show();
                        items.add(itemsTmp[count % 5]);
                        items.add(itemsTmp[count % 5]);
                        adapter.notifyDataSetChanged();
                        layout.stopPullRefresh();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RecyclerRefreshViewActivity.this, "底部刷新结束", Toast.LENGTH_SHORT).show();

                        if (count > 2) {
                            layout.stopLoadMore(RefreshState.ResultState.END);
                            return;
                        }
                        count++;
                        items.add(itemsTmp[count % 10]);
                        items.add(itemsTmp[count % 10]);
                        adapter.notifyDataSetChanged();
                        layout.stopLoadMore(RefreshState.ResultState.FAILURE);
                    }
                }, 2000);
            }
        });
    }

    boolean tag = false;

    public void onBottomClick(View view) {
        int listViewVisible = tag ? View.VISIBLE : View.GONE;
        int emptyViewVisible = !tag ? View.VISIBLE : View.GONE;
        recyclerView.setVisibility(listViewVisible);
        emptyView.setVisibility(emptyViewVisible);
        tag = !tag;
    }

    static class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
        private List<String> mDataset;


        public MyAdapter(List<String> myDataset) {
            mDataset = myDataset;
        }

        @Override
        public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item, parent, false);
            MyViewHolder vh = new MyViewHolder(v);
            return vh;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.textView.setText(mDataset.get(position));
        }

        @Override
        public int getItemCount() {
            return mDataset.size();
        }

        static class MyViewHolder extends RecyclerView.ViewHolder {
            // each data item is just a string in this case
            TextView textView;

            MyViewHolder(View v) {
                super(v);
                textView = v.findViewById(R.id.text_view);
            }
        }
    }
}
